package com.gxt.org.controller;

import com.gxt.basic.utils.AjaxResult;
import com.gxt.basic.utils.PageList;
import com.gxt.org.domain.Employee;
import com.gxt.org.query.EmployeeQuery;
import com.gxt.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
@Api(value = "员工管理controller",description = "员工管理controller")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping
    @ApiOperation(value = "查询所有")
    public AjaxResult loadAll(){
        try {
            List<Employee> lists = employeeService.loadAll();
            return AjaxResult.me().setResult(lists);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }
    }

    //新增、修改
    @PutMapping
    @ApiOperation(value = "保存员工")
    public AjaxResult save(@RequestBody Employee employee){
        try {
            if(employee.getId() != null){
                employeeService.update(employee);
            }else{
                employeeService.insert(employee);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败!"+e.getMessage());
        }
        return AjaxResult.me();
    }

    // 删除单条  delete
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除员工")
    public AjaxResult removeById(@ApiParam(required = true) @PathVariable("id")Long id){
        try {
            employeeService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
        return AjaxResult.me();
    }

    //查询单条
    @GetMapping("/{id}")
    @ApiOperation(value = "查询员工详情")
    public AjaxResult loadById(@ApiParam(required = true) @PathVariable("id")Long id){
        try {
            Employee employee = employeeService.loadById(id);
            return AjaxResult.me().setResult(employee);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    //分页查询
    @PostMapping
    @ApiOperation(value = "分页查询")
    public AjaxResult pageList(@RequestBody EmployeeQuery query){
        try {
            PageList<Employee> pageList = employeeService.pageList(query);
            return AjaxResult.me().setResult(pageList);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("查询失败!"+e.getMessage());
        }
    }

    //批量删除
    @PatchMapping
    @ApiOperation(value = "批量删除员工")
    public AjaxResult batchDelete(@ApiParam(required = true) @RequestBody List<Long> ids){
        try {
            employeeService.batchDelete(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败!"+e.getMessage());
        }
        return AjaxResult.me();
    }
}
